package com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.bumptech.glide.Glide;
import com.hyphenate.easeui.utils.map.BaseUrl;
import com.hyphenate.easeui.utils.map.JSONUtils;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.kongzue.dialog.interfaces.OnMenuItemClickListener;
import com.kongzue.dialog.v3.BottomMenu;
import com.kongzue.dialog.v3.WaitDialog;
import com.kongzue.takephoto.TakePhotoUtil;
import com.ninetaildemonfox.zdl.socialproject.base.BaseActivity;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.EditContract;
import com.ninetaildemonfox.zdl.socialproject.mvp.presenter.EditPresenter;
import com.ninetaildemonfox.zdl.socialproject.utils.RoundImageView;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author NineTailDemonFox
 * @date 2019/7/23 17:39
 * 功能描述： 编辑资料
 * 联系方式：1037438704@qq.com
 */

public class EditActivity extends BaseActivity implements EditContract {
    @BindView(R.id.image_left_finish)
    ImageView imageleftFinish;
    @BindView(R.id.image_head_portrait)
    RoundImageView imageHeadPortrait;
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.text_right)
    TextView textRight;
    @BindView(R.id.ll_birthday)
    LinearLayout llBirthday;
    @BindView(R.id.text_birthday)
    TextView textBirthday;
    @BindView(R.id.text_name)
    TextView textName;
    private String head_pic_id;

    private File file;

    private EditPresenter editPresenter;

    @Override
    protected int getLayoutResource() {
        return R.layout.edit_activity;
    }

    @Override
    protected void onInitialization(Bundle bundle) {
        editPresenter = new EditPresenter(me, this, this);
        imageleftFinish.setVisibility(View.VISIBLE);
        textCenter.setText("编辑资料");
        textRight.setVisibility(View.VISIBLE);
        textRight.setText("保存");
        editPresenter.getModBaseDateHead(token, "",
                textName.getText().toString().trim(), textBirthday.getText().toString().trim(), "2");

        TakePhotoUtil.getInstance(this).setReturnPhoto(new TakePhotoUtil.ReturnPhoto() {
            @Override
            public void onGetPhotos(String[] selectImagePaths) {
                if (MainActivity.thisMainCount == 1) {
                    //这里获取文件路径并上传
                    String selectImagePath = selectImagePaths[0];
                    //                             文件路径             1上传    2设置	              1视频                    2 图片
                    PictureUploadActivity.start(me, selectImagePath, "1", "2");
                } else if (MainActivity.thisMainCount == 4){
                    file = new File(selectImagePaths[0]);
                    Glide.with(me).load(selectImagePaths[0]).into(imageHeadPortrait);
                }
            }

            @Override
            public void onError(Exception e) {
                e.printStackTrace();
            }
        });
    }

    @OnClick({R.id.image_left_finish, R.id.image_head_portrait, R.id.ll_birthday, R.id.text_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.image_left_finish:
                finish();
                break;
            case R.id.ll_birthday:
                //时间选择器
                TimePickerView pvTime = new TimePickerBuilder(this,
                        new OnTimeSelectListener() {
                            @Override
                            public void onTimeSelect(Date date, View v) {
                                textBirthday.setText(getTime(date));
                            }
                        })
                        .setTitleText("日期选择")
                        .setSubmitText("确定")//确定按钮文字
                        .setCancelText("取消")//取消按钮文字
                        .setCancelColor(Color.GRAY)
                        .setSubmitColor(Color.RED)
                        .build();
                pvTime.show();
                break;
            case R.id.image_head_portrait:
                MainActivity.thisMainCount = 4;
                BottomMenu.show(this, new String[]{"拍照", "从相册里选择"}, new OnMenuItemClickListener() {
                    @Override
                    public void onClick(String text, int index) {
                        switch (index) {
                            case 0:
                                //使用相机拍摄
                                TakePhotoUtil.getInstance(EditActivity.this).doOpenCamera();
                                break;
                            case 1:
                                TakePhotoUtil.getInstance(EditActivity.this).doOpenGallery();
                                break;
                            default:
                        }
                    }
                });
                break;
            case R.id.text_right:
                if (file == null) {
                    editPresenter.getModBaseDateHead(token, head_pic_id, textName.getText().toString().trim(), textBirthday.getText().toString().trim(), "1");
                    return;
                }
                WaitDialog.show(me, "数据上传中...");
                HttpRequest.POST(me, BaseUrl.BaseHttpUrl + "Member/modBaseDataHead"
                        , new Parameter()
                                .add("token", token)
                                .add("head_pic_id", "")
                                .add("nickname", textName.getText().toString().trim())
                                .add("birthday", textBirthday.getText().toString().trim())
                                .add("type", "1")
                                .add("img", file)
                        , new ResponseListener() {
                            @Override
                            public void onResponse(String response, Exception error) {
                                WaitDialog.dismiss();
                                if (error == null) {
                                    Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                                    if (map.get("code").equals("1")) {
                                        finish();
                                    }
                                    ToastUtil.show(map.get("message"));
                                } else {
                                    ToastUtil.show("网络链接错误！");
                                }
                            }
                        });
                break;
            default:
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        TakePhotoUtil.getInstance(EditActivity.this).onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 时间选择器
     */
    private String getTime(Date date) {
        //可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
//        SimpleDateFormat format = new SimpleDateFormat("MM月dd日 HH:mm");
        return format.format(date);
    }

    @Override
    public void editSucces(Map<String, String> data) {
        Glide.with(me).load(data.get("head_pic")).into(imageHeadPortrait);
        textName.setText(data.get("nickname"));
        textBirthday.setText(data.get("birthday"));
        head_pic_id = data.get("head_pic_id");
    }

    @Override
    public void editFailed(String msg) {

    }

    @Override
    public void showLoading() {
        WaitDialog.show(me, "数据加载中");
    }


}
